const { request } = require('../../services/request')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      // 显示隐藏
      type: Boolean,
      value: true,
      observer: '_showChange'
    },
    groupProcessId:String

  },
  /**
   * 组件的初始数据
   */
  data: {
    feedbackList: [],
    innerShow:false,
    wrapperShow:false,
    choosing:[],
    inputText:''
  },
lifetimes:{
  attached() {
      console.log('feedback created')
    this._showChange(this.data.show)
    this.requireProblems()
    },
},
    

  /**
   * 组件的方法列表
   */
  methods: {
    _showChange(show){
      if (show) {
        this.setData({
            innerShow: true,
            wrapperShow:true
        })
    } else {
        this.setData({ innerShow: false })
        setTimeout(() => {
            this.setData({ wrapperShow: false })
        }, 300)
    }
    },
    requireProblems(){
      request('POST','/applet/dialog/feedback/list').then(res=>{
        if(res.data.length!=0){
          this.setData({feedbackList:res.data})
        }else{}
      })
    },
    chooseFeedback(e){
      const choice=e.currentTarget.dataset.index
      let choosing=this.data.choosing
      if(choosing.includes(choice)){
        let index=choosing.indexOf(choice)
        choosing.splice(index,1)
      }else{
        choosing.push(choice)
      }
      this.setData({choosing})
      console.log('chooseFeedback>>',choosing)
    },
    
    inputFeedback(e){
      const inputText=e.detail.value
      this.setData({inputText})
      console.log('inputFeedback>>',inputText)
    },
    hidePopup(){
      console.log('hide')
      this.handelSubmit()
      this.triggerEvent('hidePopup')
    },
    handelSubmit(){
      /**发请求or触发事件 */
      const params={
        groupProcessId:this.data.groupProcessId,
        likeType:2,
        unlikeReasonId:this.data.choosing,
        unlikeReason:this.inputText
      }
      request('POST','/applet/dialog/group/like',params).then(res=>{
        wx.showToast({
          title: '感谢您的反馈',
        })
        this.triggerEvent('hidePopup')
      }).catch(err=>{this.triggerEvent('hidePopup')})
    }
  },
})
